<?php 
class ModelPhoneNumber extends Model {
	public function addNumber($data = array()){
		$phone = array();
		$phone_id = 0;

		if(isset($data['number_id']))
			$phone = $this->getNumber($data['number_id']);
		else if(isset($data['number']))
			$phone = $this->getNumberByText($data['number']);

		if(empty($phone)){
			
			$query = $this->db->query("INSERT INTO phones SET number = '" . $data['number'] . "', total_comments = '1', total_search = '0', created = '".date('Y-m-d',time())."'");
			
			$phone_id = $this->db->getLastId();
			if($phone_id){
				if($data['user'] != ''){
					$nickname = $data['user'];
				}
				
			}
			$phone = $this->getNumber($phone_id);
		}
		else{
			$phone_id = $phone['phone_id'];
			$this->db->query("Update phones SET total_comments =  total_comments  + 1 Where phone_id = '" .(int)$phone_id. "'");
		}
		
		$nickname = 'anounymous';
		if(!empty($data['owner']))
			$nickname = $data['owner'];
		$ip = $_SERVER['REMOTE_ADDR'];
		$query = $this->db->query("INSERT INTO phone_comments SET phone_id = '" . $phone_id . "', call_type_id = '" . $data['call_type_id'] . "', comment = '" . $this->db->escape($data['comment']) ."', owner = '".$data['owner']."', nickname = '".$this->db->escape($nickname)."', ip = '".$ip."', created = '".date('Y-m-d h:i:s',time())."', updated = '".date('Y-m-d h:i:s',time())."'");
		return $phone;
	}
	
	public function existNumber($number = ''){
		$query = $this->db->query("SELECT phone_id from phones where number = '".$number."'");
		if( isset($query->row['phone_id']) && $query->row['phone_id'] != '')
			return true;
		else
			return false;
	}
	
	public function getNumberByText($number = ''){
		$query = $this->db->query("SELECT * FROM phones WHERE number = '" . $number . "'");
		
		return $query->row;
	}
	
	public function getNumber($phone_id) {
		$query = $this->db->query("SELECT * FROM phones WHERE phone_id = '" . (int)$phone_id . "'");
		
		return $query->row;
	}

	public function getNumbers($data = array()) {
		$sql = "SELECT * FROM phones p";

		$sort_data = array(
			'p.number',
			'p.total_search',
		);	

		if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
			$sql .= " ORDER BY " . $data['sort'];	
		} else {
			$sql .= " ORDER BY p.phone_id";	
		}

		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
		}

		if (isset($data['start']) || isset($data['limit'])) {
			if ($data['start'] < 0) {
				$data['start'] = 0;
			}				

			if ($data['limit'] < 1) {
				$data['limit'] = 20;
			}	

			$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
		}	

		$query = $this->db->query($sql);

		return $query->rows;
	}

	public function getTotalNumbers() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM phones");

		return $query->row['total'];
	}
	
	public function getTopSpam() {
		$sql = "SELECT number, total_comments from phones order by total_comments desc";
		
		$data['start'] = 0;
		$data['limit'] = 10;
		$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
	
		$query = $this->db->query($sql);
	
		return $query->rows;
	}
	
	public function addCount($phone_id = 0){
		$query = $this->db->query("Update phones set total_search = total_search + 1 where phone_id = '" . (int)$phone_id . "'");
	}
}
?>